package com.collect.tanzania.zhiScene.zhiCollection.zhiMember.zhiRoot;

import android.annotation.SuppressLint;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.drawerlayout.widget.DrawerLayout;

import android.text.TextUtils;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.collect.tanzania.R;
import com.collect.tanzania.zhiBase.BaseActivity;
import com.collect.tanzania.zhiBean.zhiSend.ZHISDailyCaseEntity;
import com.collect.tanzania.zhiBean.zhiSend.ZHISPerfermanceEntity;
import com.collect.tanzania.zhiGlobal.zhiAppSettings.ZHIProjectRouter;
import com.collect.tanzania.zhiHelper.ZHIMainProcessHelper;
import com.collect.tanzania.zhiScene.zhiCollection.zhiMember.zhiAccount.ZHIAccountMemberFragment;
import com.collect.tanzania.zhiScene.zhiCollection.zhiMember.zhiDaily.ZHIDailyMemberFragment;
import com.collect.tanzania.zhiScene.zhiCollection.zhiMember.zhiPerformance.ZHIPerformanceMemberFragment;
import com.collect.tanzania.zhiScene.zhiWidget.zhiFilterDaily.ZHIFilterDaily;
import com.collect.tanzania.zhiScene.zhiWidget.zhiFilterDaily.ZHIFilterDailyClickListener;
import com.collect.tanzania.zhiScene.zhiWidget.zhiFilterPerformance.ZHIFilterPerformanceClickListener;
import com.collect.tanzania.zhiScene.zhiWidget.zhiFilterPerformance.ZHIFilterPerformanceMember;
import com.collect.tanzania.zhiScene.zhiWidget.zhiNavigationBar.ZHIBottomTabBarItem;
import com.collect.tanzania.zhiUtils.ZHICalendarUtil;

import butterknife.BindView;

@SuppressLint("NonConstantResourceId")
@Route(path = ZHIProjectRouter.ZHI_ROOT_MEMBER)
public class ZHIRootMemberActivity extends BaseActivity implements ZHIRootMemberFragmentInterface {

    @BindView(R.id.drawer_layout)
    DrawerLayout drawer_layout;

    @BindView(R.id.drawer_filter_daily)
    ZHIFilterDaily drawer_filter_daily;

    @BindView(R.id.drawer_filter_performance)
    ZHIFilterPerformanceMember drawer_filter_performance;

    @BindView(R.id.bar_loan)
    ZHIBottomTabBarItem bar_loan;
    @BindView(R.id.bar_account)
    ZHIBottomTabBarItem bar_account;
    @BindView(R.id.bar_perfermance)
    ZHIBottomTabBarItem bar_perfermance;

    private FragmentManager mFragmentManager;
    private ZHIDailyMemberFragment mZHIDailyMemberFragment;
    private ZHIPerformanceMemberFragment mZHIPerformanceMemberFragment;
    private ZHIAccountMemberFragment mZHIAccountMemberFragment;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_root_member;
    }

    @Override
    protected void initInjector() {
    }

    @Override
    protected void initView() {
        try {
            mFragmentManager = getSupportFragmentManager();

            drawer_filter_daily.setZHIFilterDailyClickListener(new ZHIFilterDailyClickListener() {
                @Override
                public void onConfirmClick() {
                    try {
                        if (drawer_layout.isDrawerOpen(drawer_filter_daily)) {
                            drawer_layout.closeDrawer(drawer_filter_daily);
                        }

                        if (mZHIDailyMemberFragment != null) {
                            mZHIDailyMemberFragment.refreshData();
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onCloseClick() {
                    if (drawer_layout.isDrawerOpen(drawer_filter_daily)) {
                        drawer_layout.closeDrawer(drawer_filter_daily);
                    }
                }
            });
            drawer_filter_daily.setDefaultMember(ZHIMainProcessHelper.getInstance().getLoginEntity().getUserName());
            drawer_filter_daily.setDisableMemberSelect(!ZHIMainProcessHelper.getInstance().getLoginEntity().isCustomerFlag());

            drawer_filter_performance.setZHIPerformanceFilterClickListener(new ZHIFilterPerformanceClickListener() {
                @Override
                public void onConfirmClick() {
                    if (drawer_layout.isDrawerOpen(drawer_filter_performance)) {
                        drawer_layout.closeDrawer(drawer_filter_performance);
                    }

                    if (mZHIPerformanceMemberFragment != null) {
                        mZHIPerformanceMemberFragment.refreshData();
                    }
                }

                @Override
                public void onCloseClick() {
                    if (drawer_layout.isDrawerOpen(drawer_filter_performance)) {
                        drawer_layout.closeDrawer(drawer_filter_performance);
                    }
                }
            });

            initEvent();
            selecFragment(0);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onReloadGroupUserList() {
//        if (drawer_filter_performance != null) {
//            drawer_filter_performance.updateUserDataSource();
//        }
    }

    @Override
    public void onReloadDailyListForPartRepayFlag() {
        try {
            if (drawer_filter_daily != null) {
                drawer_filter_daily.setPartRepay("YES");
            }
            selecFragment(0);
            if (mZHIDailyMemberFragment != null) {
                mZHIDailyMemberFragment.refreshData();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onShowFilterDaily() {
        if (drawer_layout.isDrawerOpen(drawer_filter_daily)) {
            drawer_layout.closeDrawer(drawer_filter_daily);
        } else {
            drawer_layout.openDrawer(drawer_filter_daily);
        }
    }

    @Override
    public void onShowFilterPerformanceAmount() {

    }

    @Override
    public void onShowFilterPerformanceMember() {
        if (drawer_filter_performance != null) {
            if (drawer_layout.isDrawerOpen(drawer_filter_performance)) {
                drawer_layout.closeDrawer(drawer_filter_performance);
            } else {
                drawer_layout.openDrawer(drawer_filter_performance);
            }
        }
    }

    @Override
    public ZHISPerfermanceEntity onGetPerfermanceFilterMemberParams() {
        ZHISPerfermanceEntity zhisPerfermanceEntity = new ZHISPerfermanceEntity();
        zhisPerfermanceEntity.setLeven(drawer_filter_performance.getLevel());
        zhisPerfermanceEntity.setMonth(drawer_filter_performance.getMonth());
        zhisPerfermanceEntity.setStartDate(ZHICalendarUtil.getCurMonthFirstDayFormatString());
        zhisPerfermanceEntity.setEndDate(ZHICalendarUtil.getDateStringAfterDate(ZHICalendarUtil.getCurMonthCurDayFormatString(), 1));
        zhisPerfermanceEntity.setSysLoginName(drawer_filter_performance.getMember());
        return zhisPerfermanceEntity;
    }

    @Override
    public ZHISPerfermanceEntity onGetPerfermanceFilterAmountParams() {
        ZHISPerfermanceEntity zhisPerfermanceEntity = new ZHISPerfermanceEntity();
//        zhisPerfermanceEntity.setLeven(drawer_filter_performance.getLevel());
//        zhisPerfermanceEntity.setEndDate(drawer_filter_performance.getDueEndDate());
//        zhisPerfermanceEntity.setStartDate(drawer_filter_performance.getDudStartDate());
//        zhisPerfermanceEntity.setLeaderUserId(drawer_filter_performance.getMember());
        return zhisPerfermanceEntity;
    }

    @Override
    public ZHISPerfermanceEntity onGetPerfermanceFilterOrderParams() {
        ZHISPerfermanceEntity zhisPerfermanceEntity = new ZHISPerfermanceEntity();
//        zhisPerfermanceEntity.setLeven(drawer_filter_performance.getLevel());
//        zhisPerfermanceEntity.setEndDate(drawer_filter_performance.getDueEndDate());
//        zhisPerfermanceEntity.setStartDate(drawer_filter_performance.getDudStartDate());
//        zhisPerfermanceEntity.setLeaderUserId(drawer_filter_performance.getMember());
        return zhisPerfermanceEntity;
    }

    @Override
    public ZHISDailyCaseEntity onGetDailyFilterParams() {
        ZHISDailyCaseEntity zhisDailyCaseEntity = new ZHISDailyCaseEntity();
        zhisDailyCaseEntity.setTypeOne(drawer_filter_daily.getOrderType());
        zhisDailyCaseEntity.setTypeTwo(drawer_filter_daily.getCallType());
        zhisDailyCaseEntity.setContactResult(drawer_filter_daily.getCallResult());
        zhisDailyCaseEntity.setPtpTimeStart(drawer_filter_daily.getPromisedRepayStartDate());
        zhisDailyCaseEntity.setPtpTimeEnd(drawer_filter_daily.getPromisedRepayEndDate());
        zhisDailyCaseEntity.setRepayStartTime(drawer_filter_daily.getDudStartDate());
        zhisDailyCaseEntity.setRepayEndTime(drawer_filter_daily.getDueEndDate());
        zhisDailyCaseEntity.setCalcType(drawer_filter_daily.getCompareFlag());
        zhisDailyCaseEntity.setOverdueDay(drawer_filter_daily.getCompareOverdueDays());
        zhisDailyCaseEntity.setMobile(drawer_filter_daily.getUserMobile());
        zhisDailyCaseEntity.setPartRepayFlag(drawer_filter_daily.getPartRepay());

        //如果不选择成员，不用赋值，使用默认
        if (!TextUtils.isEmpty(drawer_filter_daily.getMember())) {
            zhisDailyCaseEntity.setSysLoginName(drawer_filter_daily.getMember());
        }

        return zhisDailyCaseEntity;
    }

    public void initEvent() {
        bar_loan.setCFOnMenuItemDidChangedListener(item -> {
            bar_perfermance.setChecked(false);
            bar_account.setChecked(false);
            selecFragment(0);
        });
        bar_perfermance.setCFOnMenuItemDidChangedListener(item -> {
            bar_loan.setChecked(false);
            bar_account.setChecked(false);
            selecFragment(1);
        });
        bar_account.setCFOnMenuItemDidChangedListener(item -> {
            bar_loan.setChecked(false);
            bar_perfermance.setChecked(false);
            selecFragment(2);
        });
    }

    private void selecFragment(int indext) {
        FragmentTransaction fragemntTransaction = mFragmentManager.beginTransaction();
        hideFragment(fragemntTransaction);
        switch (indext) {
            case 0: {
                bar_loan.setChecked(true);
                bar_perfermance.setChecked(false);
                bar_account.setChecked(false);
                if (null == mZHIDailyMemberFragment) {
                    mZHIDailyMemberFragment = new ZHIDailyMemberFragment();
                    fragemntTransaction.add(R.id.content, mZHIDailyMemberFragment);
                } else {
                    fragemntTransaction.show(mZHIDailyMemberFragment);
                }
                break;
            }
            case 1: {
                bar_loan.setChecked(false);
                bar_perfermance.setChecked(true);
                bar_account.setChecked(false);
                if (null == mZHIPerformanceMemberFragment) {
                    mZHIPerformanceMemberFragment = new ZHIPerformanceMemberFragment();
                    fragemntTransaction.add(R.id.content, mZHIPerformanceMemberFragment);
                } else {
                    fragemntTransaction.show(mZHIPerformanceMemberFragment);
                }
                break;
            }
            case 2: {
                bar_loan.setChecked(false);
                bar_perfermance.setChecked(false);
                bar_account.setChecked(true);
                if (null == mZHIAccountMemberFragment) {
                    mZHIAccountMemberFragment = new ZHIAccountMemberFragment();
                    fragemntTransaction.add(R.id.content, mZHIAccountMemberFragment);
                } else {
                    fragemntTransaction.show(mZHIAccountMemberFragment);
                }
                break;
            }
            default:
                break;
        }
        fragemntTransaction.commitAllowingStateLoss();
    }

    private void hideFragment(FragmentTransaction fragemntTransaction) {
        if (fragemntTransaction == null) return;

        if (null != mZHIDailyMemberFragment) {
            fragemntTransaction.hide(mZHIDailyMemberFragment);
        }
        if (null != mZHIAccountMemberFragment) {
            fragemntTransaction.hide(mZHIAccountMemberFragment);
        }

        if (null != mZHIPerformanceMemberFragment) {
            fragemntTransaction.hide(mZHIPerformanceMemberFragment);
        }
    }
}
